<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 会员结账单据控制器
 * @package app\index\controller
 */
class UserCheckOut extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取结账单据
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if(!isset($getData['limit']) || empty($getData['limit'])){
                $getData['limit'] = '10';
            }

            $sql = Db::name('qt_check_out')
                ->alias('c')
                ->leftJoin('qt_user_viper uv','uv.id = c.uv_id')
                ->leftJoin('qt_viper_card vc','vc.id = c.viper_card_id')
                ->field('c.id,c.order_no,uv.nickname,uv.phone,c.cashier_id,c.cashier_time,c.is_open,c.open_id,c.open_time,c.open_content,vc.card_no')
                ->where(['c.store_id' => $userInfo['store_id']])
                ->order('c.id desc');
            //单号查询
            if (isset($getData['order_no']) && !empty($getData['order_no']))
            {
                $sql = $sql->where('c.order_no',$getData['order_no']);
            }
            if (isset($data['start_time']) && !empty($getData['start_time']))
            {
                $sql = $sql->whereTime('c.cashier_time','>',strtotime($getData['start_time']));
            }
            if (isset($getData['end_time']) && !empty($getData['end_time']))
            {
                $sql = $sql->whereTime('c.cashier_time','<',strtotime($getData['start_time']));
            }
            //会员卡类型
            if (isset($getData['viper_type_id']) && !empty($getData['viper_type_id']))
            {
                $sql = $sql->where('vc.viper_type_id',$getData['viper_type_id']);
            }
            //会员卡类型
            if (isset($getData['type']) && !empty($getData['type']))
            {
                if($getData['type'] == '2'){
                    $sql = $sql->whereIn('vc.status',['1','2']);
                }
            }
            $data = $sql->paginate($getData['limit'])->each(function ($item){
                    $cashierName =  Db::name('qt_users')->where('id',$item['cashier_id'])->value('real_name');
                    $openName =  Db::name('qt_users')->where('id',$item['open_id'])->value('real_name');
                    $item['cashier_name'] = $cashierName;
                    $item['open_name'] = $openName;
                    return $item;
                });
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 会员消费开票
     */
    public function open_billing(){
        $data = $this->request->post();
        if(!isset($data['id']) || empty($data['id'])){
            $this->result('缺少参数','',204);
        }
        if($data['type'] == '1'){
            //开票
            $update['is_open'] = '2';
            $update['open_id'] = $this->userInfo['id'];
            $update['open_content'] = $data['open_content'];
            $update['open_time'] = time();
        }
        if($data['type'] == '2'){
            //撤销接口
            $update['is_open'] = '1';
            $update['open_id'] = '';
            $update['open_time'] = null;
        }
        $already = Db::name('qt_check_out')->where(['id'=>$data['id']])->update($update);
        if($already !== false){
            $this->result('操作成功','',200);
        }
        $this->result('操作失败','',204);
    }





    /**
     * 添加积分模板
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            $validate = new \app\web\validate\ViperIntegral();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['create_time'] = time();
            $getData['store_id'] = $this->userInfo['store_id'];
            $insert = Db::name('qt_viper_integral_exchange')->insert($getData);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑保存积分模板
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            $validate = new \app\web\validate\ViperIntegral();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $update= Db::name('qt_viper_integral_exchange')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑保存积分模板
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_viper_integral_exchange')
                ->field('id,name,integral,money,goods_name,vip_type_id')
                ->where('id',$getData['id'])->find();
            $viperIds = explode(',',$data['vip_type_id']);
            $viperType = Db::name('qt_viper_type')->whereIn('id',$viperIds)->column('name');
            $data['viper_type_name'] = implode(',',$viperType);
            $this->result('获取成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除积分模板
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            Db::name('qt_viper_integral_exchange')->where('id',$getData['id'])->delete();
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }



}
